/*******************************************************************************
 *
 * DSM - Distributed Search Manager
 * Developed by Milspec Research International Pty Ltd
 *
 * $Author: gsingh $
 * $Revision: 1.4 $
 * $Date: 2005/09/27 05:28:06 $
 *
 * (c)Copyright 2004
 *    education.au limited
 *    DEST
 *    All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the names education.au limited, DEST nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * To the extent permitted by law, the copyright owners of this software and
 * its contributors:
 *
 *  (i) exclude all warranties in relation to the software; and
 * (ii) exclude liability for all losses, costs, expenses and damages arising
 *      in any way from the use of the software whether arising from or in
 *      relation to breach of contract, negligence or any other tort, in equity
 *      or otherwise. If the software is in breach of a warranty which is
 *      implied by law, the copyright owners and contributors liability is
 *      limited to the replacement of the software.
 *
 *******************************************************************************
 */

package au.edu.educationau.opensource.dsm.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Enumeration;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * The DSM Properties Manager. Loads and holds the search manager's properties
 * in the System object
 */

public final class PropertiesService {
	static Logger logger = Logger.getLogger(PropertiesService.class.getClass());

	/**
	 * Loads the properties from the propertiesFile
	 * 
	 * @param propertiesPath
	 *            the path to the properties file as sent by web.xml
	 */
	public synchronized static void loadProperties(String propertiesPath) {
		try {
			logger.info("Starting properties service...");
			File propFile = new File(propertiesPath);
			if (!propFile.canRead()) {
				logger.error("Properties file " + propFile.getAbsolutePath() + " not found. Resorting to defaults.");
				return;
			}
			BufferedReader reader = new BufferedReader(new FileReader(propFile));
			while (reader.ready()) {
				String propLine = reader.readLine();
				if (propLine.length() > 0 && !propLine.startsWith("#") && propLine.indexOf("=") > 0) {
					String propName = propLine.substring(0, propLine.indexOf("=")).trim();
					String propValue = propLine.substring(propLine.indexOf("=") + 1).trim();
					System.setProperty("DSM4-" + propName, propValue);
				} // if
			}
			reader.close();
			logger.info("PropertiesService Started OK using properties file " + propertiesPath);
		} // try
		catch (Exception oe) {
			logger.error("PropertiesService Failed. Properties has error in it!", oe);
		} // catch
	}

	/**
	 * Obtain a String type property value
	 * 
	 * @param propertyName
	 *            the name of this property
	 * @param defaultValue
	 *            the default value to use if the property does not exist
	 */
	public static String getStringProperty(String propertyName, String defaultValue) {
		String prop = System.getProperty("DSM4-" + propertyName);
		if (null == prop || prop.length() <= 0) {
			return defaultValue;
		} else {
			return prop;
		}
	}

	/**
	 * Obtain a integer type property value
	 * 
	 * @param propertyName
	 *            the name of this property
	 * @param defaultValue
	 *            the default value to use if the property does not exist
	 */
	public static int getIntegerProperty(String propertyName, int defaultValue) {
		return (Integer.getInteger("DSM4-" + propertyName, defaultValue)).intValue();
	}

	/**
	 * Obtain a long integer type property value
	 * 
	 * @param propertyName
	 *            the name of this property
	 * @param defaultValue
	 *            the default value to use if the property does not exist
	 */
	public static long getLongProperty(String propertyName, long defaultValue) {
		return (Long.getLong("DSM4-" + propertyName, defaultValue)).longValue();
	}

	/**
	 * Obtain a Float type property value
	 * 
	 * @param propertyName
	 *            the name of this property
	 * @param defaultValue
	 *            the default value to use if the property does not exist
	 */
	public static float getFloatProperty(String propertyName, float defaultValue) {
		String prop = System.getProperty("DSM4-" + propertyName);
		try {
			return Float.parseFloat(prop);
		} catch (Exception oe) {
			return defaultValue;
		}
	}

	/**
	 * Obtain a Boolean type property value
	 * 
	 * @param propertyName
	 *            the name of this property
	 * @param defaultValue
	 *            the default value to use if the property does not exist
	 */
	public static boolean getBooleanProperty(String propertyName, boolean defaultValue) {
		return Boolean.getBoolean("DSM4-" + propertyName);
	}

	/**
	 * Returns all properties with that prefix.
	 * 
	 */
	public static Properties getAllProperties(String prefix) {
		Properties p = new Properties();
		for (Enumeration e = System.getProperties().propertyNames(); e.hasMoreElements();) {
			String property = (String) e.nextElement();
			if (null != property && property.startsWith("DSM4-" + prefix)) {
				p.setProperty(property.substring(5 + prefix.length() + 1), System.getProperty(property)); // the
																											// DSM-4+the
																											// prefix
																											// +
																											// the
																											// dot
			}
		}
		return p;
	}

}

// -- EOF
